﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Maticsoft.DAL;
using System.Reflection;
using System.IO;


namespace BuildingForm.PersonInfo
{
    public partial class EmployeeEntryExit : Form
    {
        public EmployeeEntryExit()
        {
            InitializeComponent();
            Init();
            bind();
        }
         private void Init()
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            //表头加粗，居中
            foreach (DataGridViewColumn col in this.dataGridView1.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.dataGridView1.AllowUserToAddRows = false;
            //班组
            Maticsoft.DAL.Banz_Info info = new Banz_Info();
            DataTable dt = info.GetList("").Tables[0];
            List<string> strList = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strList.Add(dt.Rows[i]["bzmc"].ToString().Trim());
            }
            List<string> list = new List<string>();
            list.Add("全部");
            list.AddRange(strList);
            this.cboTeam.DataSource = list;
            //年，月
            int[] months = new int[12] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            this.cboMonth.DataSource = months;
            this.cboMonth.Text = DateTime.Now.Month.ToString();
            int year = DateTime.Now.Year;
            int[] years = new int[12] { year-11,year-10,year - 9, year - 8, year - 7, year - 6, year - 5, year - 4, year - 3, year - 2, year - 1, year };
            this.cboYear.DataSource = years;
            this.cboYear.Text = DateTime.Now.Year.ToString();
        }

         private DataTable bind()
         {
             Maticsoft.DAL.Kqjl_Info info = new Kqjl_Info();
             string sql = string.Empty;
             sql = " 1=1 ";
             string team = this.cboTeam.Text.ToString().Trim();
             if (team != "全部")
             {
                 sql += " and c.bzmc='" + team + "'";
             }
             int year = Convert.ToInt32(this.cboYear.Text.ToString());
             int month = Convert.ToInt32(this.cboMonth.Text.ToString());
             string strT = year + "/" + month + "/" + "01";
             string time = Convert.ToDateTime(strT).AddMonths(1).AddMilliseconds(-1).ToString("yyyy-MM-dd HH:mm:ss:fff");
             sql += " and d.fksj<='" + time + "'";
             sql += " order by a.createtime desc ";
             DataTable dt = info.GetListByEmployeeEntryExit(sql).Tables[0];
             this.dataGridView1.AutoGenerateColumns = false;
             this.dataGridView1.DataSource = dt;
             return dt;
         }

         private void button1_Click(object sender, EventArgs e)
         {
             bind();
         }

         private void btnExport_Click(object sender, EventArgs e)
         {
             try
             {
                 Sys_Info dao = new Sys_Info();
                 string name = string.Empty;
                 DataTable dtt = dao.GetList("").Tables[0];
                 if (dtt.Rows.Count > 0)
                 {
                     name = dtt.Rows[0]["gcmc"].ToString();
                 }
                 DataTable dt = bind();
                 if (dt.Rows.Count==0)
                 {
                     MessageBox.Show("没有数据！"); return;
                 }
                 SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                 //设置文件类型   
                 saveFileDialog1.Filter = " xls files(*.xls)|*.xls";
                 //设置默认文件类型显示顺序   
                 saveFileDialog1.FilterIndex = 2;
                 //保存对话框是否记忆上次打开的目录   
                 saveFileDialog1.RestoreDirectory = true;
                 saveFileDialog1.FileName = "劳务人员实名登记台账" + DateTime.Now.ToString("yyyyMMdd");
                 //点了保存按钮进入   
                 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                 {
                     string team = this.cboTeam.Text.ToString().Trim();
                     if (team == "全部") { team = "所有人员"; };
                     team = team + "," + name;
                     string fileName = saveFileDialog1.FileName;
                     string[] oldCols = new string[] { "xh",  "xm", "xb","bzmc", "gz","jtzz", "sfzh","bank","aa","bb", "intime2", "outtime", "cc","dd","lxdh","address", "remark" };
                     string[] newCols = new string[] { "序号", "姓名",  "性别","班组", "工种","家庭住址", "身份证号", "银行（工资）卡开户行及卡号","劳动合同登记","安全培训情况",   "进场时间", "退场时间","退场工资结清情况","持上岗证情况","联系电话","住宿", "备注" };
                     ExcelHelper.ExportByEmployeeEntryExit(dt, "劳务人员实名登记台账", fileName, "Sheet1", oldCols, newCols, team);
                     MessageBox.Show("导出文件成功！"); return;
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show("导出文件故障！"); return;
             }
         }

         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
         {

         }

         private void EmployeeEntryExit_Load(object sender, EventArgs e)
         {

         }

    }
}
